//app.js
var util = require('./utils/myutils.js');
App({
  // testUrl: 'http://127.0.0.1:9088',
  testUrl: 'https://learn.gotohard.cn',
  globalData: {
    userInfo: {}
  },
  getc: util,
  onLaunch: function () {
    console.log(this.dateto("2018-05-01 08:58:26"))
    console.log("开始启动")
    // wx.clearStorage()
    var app = this
    wx.getStorage({
      key: 'user',
      success: function (res) {
        console.log(res.data)
        var app = getApp()
        console.log(app)
        app.globalData.userInfo = res.data.userinfo
        app.globalData.openid = res.data.openid
        console.log(app)
      },
      fail: function () {
        console.log("获取缓存失败")
        console.log(app)
        app.getset();
      }
    })
  },

  getset: function () {
    var user = { info: {} }
    // 获取用户信息
    wx.getSetting({
      success: function (res) {
        console.log("获取设置信息成功")
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log("获取用户信息成功")
              // var that = getApp();
              console.log(res.userInfo)
              user.info.userinfo = res.userInfo
              // 登录
              wx.login({
                success: res => {
                  console.log(res)
                  // var that = getApp();
                  // that.code = res.code
                  user.code = res.code
                  var app = getApp()
                  var testurl = app.testUrl
                  wx.request({
                    url: testurl + '/user/getcode.do?code=',
                    data: {
                      code: res.code
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                      var app = getApp()
                      console.log(res)
                      user.info.openid = res.data.openid
                      user.info.userinfo.area = app.getc.getChina(user.info.userinfo.province) + " " + app.getc.getChina(user.info.userinfo.city),
                        console.log(user)
                      wx.setStorage({
                        key: "user",
                        data: user.info
                      })

                      // 设置用户信息
                      app.globalData.userInfo = user.info.userinfo
                      app.globalData.openid = user.info.openid
                      wx.request({
                        url: testurl + '/user/add.do',
                        method: "POST",
                        data: {
                          wxOpenid: user.info.openid,
                          wxUsername: user.info.userinfo.nickName,
                          userImg: user.info.userinfo.avatarUrl,
                          area: user.info.userinfo.area,
                          username: user.info.userinfo.nickName
                        },
                        header: {
                          'content-type': 'application/json' // 默认值
                        },
                        success: function (res) {
                          console.log(res)
                          var app = getApp()
                          console.log(app)
                        }
                      })

                    }
                  })
                  // 发送 res.code 到后台换取 openId, sessionKey, unionId
                },
                fail: function (res) {
                  console.log(res + "失败")
                }
              })


            },
            fail: function (e) {
              console.log(e + "获取用户信息失败")
            }
          })
        } else {
          console.log('没有获取授权')
        }
      },
      fail: function () {
        console.log("获取设置信息失败")
      }
    })


  },

  dateto: function (dateTimeStamp) {
    // var stringTime = "2014-07-10 10:21:12";
    // var timestamp2 = Date.parse(new Date(dateTimeStamp));
    dateTimeStamp = Date.parse(new Date(dateTimeStamp));
    // dateTimeStamp = timestamp2 / 1000;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var halfamonth = day * 15;
    var month = day * 30;
    var now = new Date().getTime();
    var diffValue = now - dateTimeStamp;
    if (diffValue < 0) { return; }
    var monthC = diffValue / month;
    var weekC = diffValue / (7 * day);
    var dayC = diffValue / day;
    var hourC = diffValue / hour;
    var minC = diffValue / minute;
    var result = ''
    if (monthC >= 1) {
      result = "" + parseInt(monthC) + "月前";
    }
    else if (weekC >= 1) {
      result = "" + parseInt(weekC) + "周前";
    }
    else if (dayC >= 1) {
      result = "" + parseInt(dayC) + "天前";
    }
    else if (hourC >= 1) {
      result = "" + parseInt(hourC) + "小时前";
    }
    else if (minC >= 1) {
      result = "" + parseInt(minC) + "分钟前";
    } else
      result = "刚刚";
    return result;

  }
})